import xlwt
class Problem():
    problemList = []
    def __init__(self, stem, option, ans, analysis='', type='', difficulty='中', chapter=''):
        self.stem = stem   # 题干
        self.option = option    # 选项
        self.ans = ans    # 答案
        self.analysis = analysis  # 解析
        self.type = type      # 题目类型
        self.difficulty = difficulty
        self.chapter = chapter
        if not type in ['单选题', '多选题', '判断题', '填空题']:
            length = len(self.option)
            if length == 2:
                self.type = '判断题'
            elif length == 1:
                self.type = '填空题'
            else:
                if len(self.ans) == 1:
                    self.type = '单选题'
                else:
                    self.type = '多选题'
        Problem.problemList.append(self)
    def __eq__(self, other):
        if self.stem == other.stem:
            if self.option == other.option:
                if self.ans == other.ans:
                    if self.type == other.type:
                        if self.analysis == other.analysis:
                            return True
        return False
    def display(self):
        print('题干', self.stem)
        print('选项', self.option)
        print('答案', self.ans)
        print('解析', self.analysis)
        print('题型', self.type, end=' 难度')
        print(self.difficulty)
    @classmethod
    def pop(cls):
        cls.problemList.pop()
    @classmethod
    def duplicateRemoval(cls):  # 题目去重
        tmp = set(cls.problemList)
        cls.problemList = list(tmp)
    @classmethod
    def clear(cls):   # 清空题目列表
        for i in cls.problemList:
            del i
        cls.problemList = []
    @classmethod
    def problemAdd(cls, problem):
        cls.problemList.add(problem)
    @classmethod
    def write(cls, format='', name='output.xls'):
        if format == '':
            format = ['题干', '题型', '选项 A', '选项 B', '选项 C', '选项 D', '选项E', '选项F', '选项G', '选项H', '正确答案','解析','章节','难度']
        work = xlwt.Workbook()  # 创建excel工作簿
        sheet_count = 0
        sheet = work.add_sheet(f'Sheet{sheet_count}')  # 创建工作表
        ptx = 0  # 工作表x行指针
        for i in format:
            sheet.write(ptx, format.index(i), i)
        ptx += 1

        # format=['题干', '题型', '选项 A', '选项 B', '选项 C', '选项 D', '选项E', '选项F', '选项G', '选项H', '正确答案','解析','章节','难度']
        for pblm in Problem.problemList:
            # print(pblm.option)
            optionCopy = pblm.option.copy()[::-1]
            for j in format:
                if j == '题干':
                    sheet.write(ptx, format.index(j), pblm.stem)
                elif j == '题型':
                    sheet.write(ptx, format.index(j), pblm.type)
                elif j == '难度':
                    sheet.write(ptx, format.index(j), pblm.difficulty)
                elif '选项' in j:
                    try:
                        sheet.write(ptx, format.index(j), optionCopy.pop())
                    except:
                        pass
                elif '答案' in j:
                    sheet.write(ptx, format.index(j), pblm.ans)
                elif '解析' in j:
                    sheet.write(ptx, format.index(j), pblm.analysis)
                elif '章节' in j:
                    sheet.write(ptx, format.index(j), pblm.chapter)
            ptx += 1
        work.save(name)


